package com.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.IntStream;

import static org.reflections.ReflectionUtils.getFields;

public class EasyExcelUtil {

    public static <T> void writeExcelWithModel(OutputStream outputStream, Class<T> clazz) throws IOException {
        EasyExcel.write(outputStream, clazz)
                .registerWriteHandler(new DropDownHandler(getDropDownMap(clazz)))
                .sheet("模板")
                .doWrite(new ArrayDeque<>());
    }

    public static Map<Integer, String[]> getDropDownMap(Class clazz) {
        Map<Integer, String[]> map = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        int col = 0;
        for (int i = 0; i < fields.length; i++) {
            ExcelProperty excelProperty = fields[i].getAnnotation(ExcelProperty.class);
            ExcelDropDown excelDropDown = fields[i].getAnnotation(ExcelDropDown.class);
            if (null != excelProperty) {
                if (null != excelDropDown) {
                    map.put(-1 == excelProperty.index() ? col : excelProperty.index(), getColDropDownMap(excelDropDown));
                }
                col++;
            }
        }
        return map;
    }

    private static String[] getColDropDownMap(ExcelDropDown excelDropDown) {
        String[] dropdown = excelDropDown.dropdown();
        // TODO
        return dropdown;
    }
}
